package ModelLayer;
import java.util.ArrayList;

public class AddressBook
{
    private ArrayList<Person> Person;
    private static AddressBook instance;
    //instance
    /**
     * Constructor for objects of class AddressBook
     */
    public AddressBook()
    {
        Person = new ArrayList<Person>();
    }

    public static AddressBook getInstance() 
    //calls the constructor
    {
        if( null == instance )
        {
            instance = new AddressBook();
        }
        return instance;
    }
    public void addPerson(String id, String name, String address, String postalCode, String city, String phone)
    //add person by this method
    {
        Person.add( new Person (id, name, address, postalCode, city, phone));
    }
    public ArrayList<Person> getPerson()
    //get person
    {
        return Person;
    }
    public void deletePerson(String id) 
    //delete person by ID
    {
      int index = Person.indexOf( findPerson(id) ) ;
      Person.remove(index);                
    }
    public Person findPerson(String id) 
    //find person by ID
    {   
        for(int index = 0; index < Person.size(); index++)
        { 
            if( Person.get(index).getId().equals(id) )
            {
                
                return Person.get(index);
            }
        }
        return null;
    }
    public void updatePerson(Person person, String name, String address, String postalCode, String city, String phone)
    //update person
    {
        person.setName(name);
        person.setAddress(address);
        person.setPostalCode(postalCode);
        person.setCity(city);
        person.setPhone(phone);
    }
}

